﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.Micro;
using FinalCountdown.Controls;
using FinalCountdownServices;
using FinalCountdownServices.Models;
using Microsoft.Phone.Shell;

namespace FinalCountdown.ViewModels
{
    public class MainPageViewModel : Screen
    {
        #region ctor + private fields

        private readonly INavigationService _navigationService;
        private readonly IEventService _eventService;
        private readonly ITileService _tileService;

        public MainPageViewModel(INavigationService navigationService, IEventService eventService, ITileService tileService)
        {
            DisplayName = "Események";
            _navigationService = navigationService;
            _eventService = eventService;
            _tileService = tileService;

            _tileService.UpdateAllAndReSchedule(_eventService.MyEvents);

        }

        #endregion

        private ObservableCollection<EventModel> _myEvents;
        public ObservableCollection<EventModel> MyEvents
        {
            get { return _myEvents ?? (_myEvents = new ObservableCollection<EventModel>()); }
            set
            {
                if (_myEvents == value) return;
                _myEvents = value;
                NotifyOfPropertyChange(() => MyEvents);
                NotifyOfPropertyChange(() => HasEvents);
            }
        }

        public bool HasEvents
        {
            get
            {
                return MyEvents.Count > 0;
            }
        }


        protected override void OnActivate()
        {
            base.OnInitialize();
            Init();
        }

        public void Init()
        {
            #region peldaesemenyek
            //_eventService.AddEvent(new EventModel()
            //{
            //    Title = "Alma esemény",
            //    Description = "Ez egy alma esemény, amit indításkor hozzáadok",
            //    TileEnabled = false,
            //    NotificationEnabled = true,
            //    NotificationMinutes = 10,
            //    Occurs = DateTime.Parse("2012-03-31 12:00")
            //});

            //_eventService.AddEvent(new EventModel()
            //{
            //    Title = "Korte esemeny",
            //    Description = "Ezt a körte eseményt indításkor adtam hozzá",
            //    TileEnabled = true,
            //    NotificationEnabled = true,
            //    NotificationMinutes = 10,
            //    Occurs = DateTime.Parse("2012-04-10 12:00")
            //});

            //_eventService.AddEvent(new EventModel()
            //{
            //    Title = "Regi esemeny",
            //    Description = "Ez az esemény már régen lejárt",
            //    TileEnabled = false,
            //    NotificationEnabled = true,
            //    NotificationMinutes = 10,
            //    Occurs = DateTime.Parse("2011-04-10 12:00")
            //});

            #endregion

            if (MyEvents == null)
                MyEvents = new ObservableCollection<EventModel>();
            if (MyEvents.Count > 0)
                MyEvents.Clear();

            foreach (var e in _eventService.MyEvents)
            {
                MyEvents.Add(e);
            }
            NotifyOfPropertyChange(() => HasEvents);
        }

        public void Pin(EventModel model)
        {
            _tileService.AddOrModifyTileByEvent(model);
        }

        public void UnPin(EventModel model)
        {
            _tileService.RemoveTileById(model.Id.Value);
            if (MyEvents != null)
            {
                var modelInList = MyEvents.FirstOrDefault(x => x.Id == model.Id);
                if (modelInList != null)
                    modelInList.Refresh();
            }
        }

        public void DeleteEvent(EventModel model)
        {
            var p = new Prompt("Biztosan törli?", () =>
            {
                MyEvents.Remove(model);
                _eventService.RemoveEvent(model.Id.Value);
                NotifyOfPropertyChange(() => HasEvents);
            });
            p.Show();
        }

        public void ShowEvent(EventModel model)
        {
            _navigationService.UriFor<EventPageViewModel>()
                .WithParam(x => x.EventId, model.Id.Value)
                .Navigate();
        }

        public void ModifyEvent(EventModel model)
        {
            _navigationService.UriFor<CreateOrModifyEventPageViewModel>()
             .WithParam(x => x.Mode, ModeType.Modify)
             .WithParam(x => x.EventId, model.Id)
             .Navigate();
        }


        public void AddNewEvent()
        {
            _navigationService.UriFor<CreateOrModifyEventPageViewModel>()
                .WithParam(x => x.Mode, ModeType.Create)
                .Navigate();
        }
    }
}

